package com.sskj.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.router.provider.LogoutProvider;
import com.sskj.lib.util.HideUtil;
import com.sskj.mine.component.DaggerUserDataComponent;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * @author Hey
 * Create at  2019/05/01
 */
@Route(path = RConfig.MINE_SET_PAY_PWD)
public class SettingPasswordActivity extends BaseActivity<SettingPasswordPresenter> {

    @BindView(R2.id.password_name)
    TextView passwordName;
    @BindView(R2.id.password_edt)
    EditText passwordEdt;
    @BindView(R2.id.old_pwd)
    EditText oldPwd;
    @BindView(R2.id.password_name2)
    TextView passwordName2;
    @BindView(R2.id.password_edt2)
    EditText passwordEdt2;
    @BindView(R2.id.mobile_name)
    TextView mobileName;
    @BindView(R2.id.mobile_edt)
    EditText mobileEdt;
    @BindView(R2.id.code_edt)
    EditText codeEdt;
    @BindView(R2.id.view1)
    View view1;
    @BindView(R2.id.get_code_tv)
    TextView getCodeTv;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    @Inject
    UserViewModel userViewModel;

    /**
     * 0-> 修改登录密码
     * 1->设置资金密码
     * 2->修改资金密码
     */
    @Autowired
    int type = 1;
    String mobile;
    String name;
    String name2;
    String hint;
    String hint2;
    @BindView(R2.id.num_phone)
    LinearLayout numPhone;
    @BindView(R2.id.code_phone)
    LinearLayout codePhone;
    @BindView(R2.id.old_pwd_ll)
    LinearLayout oldPwdLl;
    private DisposableSubscriber<Long> disposableSubscriber;
    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_setting_password;
    }

    @Override
    public SettingPasswordPresenter getPresenter() {
        return new SettingPasswordPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);

        ARouter.getInstance().inject(this);
        mobileName.setText(SPUtil.get(SPConfig.USER_ACCOUNT, "").contains("@") ? App.INSTANCE.getString(R.string.mine_settingPasswordActivity1_j) : App.INSTANCE.getString(R.string.mine_settingPasswordActivity2_j));
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                mobile = userData.getMobile();
            } else {
                userData = null;
            }
        });
        switch (type) {
            case 0:
                setTitle(App.INSTANCE.getString(R.string.mine_settingPasswordActivity3_j));
                name = App.INSTANCE.getString(R.string.mine_settingPasswordActivity4_j);
                name2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity5_j);
                hint = App.INSTANCE.getString(R.string.mine_settingPasswordActivity6_j);
                hint2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity6_j);
                oldPwdLl.setVisibility(View.VISIBLE);
                view1.setVisibility(View.GONE);
                numPhone.setVisibility(View.GONE);
                codePhone.setVisibility(View.GONE);
                break;
            case 1:
                setTitle(App.INSTANCE.getString(R.string.mine_settingPasswordActivity7_j));
                name = App.INSTANCE.getString(R.string.mine_settingPasswordActivity8_j);
                name2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity5_j);
                hint = App.INSTANCE.getString(R.string.mine_settingPasswordActivity9_j);
                hint2 = App.INSTANCE.getString(R.string.mine_transferActivity4_j);
                oldPwdLl.setVisibility(View.GONE);
                numPhone.setVisibility(View.VISIBLE);
                view1.setVisibility(View.VISIBLE);
                codePhone.setVisibility(View.VISIBLE);
                break;
            case 2:
                setTitle(App.INSTANCE.getString(R.string.mine_settingPasswordActivity11_j));
                name = App.INSTANCE.getString(R.string.mine_settingPasswordActivity8_j);
                name2 = App.INSTANCE.getString(R.string.mine_settingPasswordActivity5_j);
                hint = App.INSTANCE.getString(R.string.mine_settingPasswordActivity9_j);
                hint2 = App.INSTANCE.getString(R.string.mine_transferActivity4_j);
                oldPwdLl.setVisibility(View.GONE);
                numPhone.setVisibility(View.VISIBLE);
                view1.setVisibility(View.VISIBLE);
                codePhone.setVisibility(View.VISIBLE);
                break;
        }
        mobileEdt.setText(SPUtil.get(SPConfig.USER_ACCOUNT, "").contains("@") ? HideUtil.getEmialAddress(SPUtil.get(SPConfig.USER_ACCOUNT, "")) : HideUtil.getPhoneHide(SPUtil.get(SPConfig.USER_ACCOUNT, "")));
        passwordName.setText(name);
        passwordName2.setText(name2);
        passwordEdt.setHint(hint);
        passwordEdt2.setHint(hint2);
        oldPwd.setHint(App.INSTANCE.getString(R.string.mine_settingPasswordActivity12_j));
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initEvent() {
        ClickUtil.click(getCodeTv, () -> {
            if (SPUtil.get(SPConfig.USER_ACCOUNT, "").contains("@")) {
                mPresenter.sendEmail(SPUtil.get(SPConfig.USER_ACCOUNT, ""));
                return;
            }
            mPresenter.requestSms(SPUtil.get(SPConfig.USER_ACCOUNT, ""));
        });

        ClickUtil.click(submitBtn, () -> {
            if (type == 0) {
                if (isEmpty(oldPwd)) {
                    ToastUtil.showShort(hint);
                    return;
                }
            }
            if (isEmpty(passwordEdt)) {
                ToastUtil.showShort(hint);
                return;
            }
            if (isEmpty(passwordEdt2)) {
                ToastUtil.showShort(hint2);
                return;
            }
            if (!CheckUtil.isPayPwd(getText(passwordEdt))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_settingPasswordActivity13_j));
                return;
            }
            if (!getText(passwordEdt).equals(getText(passwordEdt2))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_settingPasswordActivity14_j));
                return;
            }
            if (type != 0) {
                if (isEmpty(codeEdt)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_settingPasswordActivity15_j));
                    return;
                }
            }

            mPresenter.setPayPwd(getText(oldPwd), getText(passwordEdt), getText(codeEdt), 0 != type);
        });
    }

    public void onAuthCodeSuccess() {
        startCount();
    }

    public void startCount() {
        getCodeTv.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                if (getCodeTv != null) {
                    getCodeTv.setText(time + App.INSTANCE.getString(R.string.mine_settingPasswordActivity16_j));
                }
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                if (getCodeTv != null) {
                    getCodeTv.setText(App.INSTANCE.getString(R.string.mine_withdrawActivity8_j));
                    getCodeTv.setEnabled(true);
                }
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    public void updataUI() {
        if (type == 0) {
            LogoutProvider logoutProvider = ARouter.getInstance().navigation(LogoutProvider.class);
            logoutProvider.logout();
        } else {
            finish();
        }
    }

}
